package com.gz.steak.modules.system.service.impl;

import com.gz.steak.common.exception.BusinessException;
import com.gz.steak.common.exception.EmBusinessError;
import com.gz.steak.modules.system.pojo.model.LoginModel;
import com.gz.steak.utils.format.DateFormat;
import com.gz.steak.modules.system.mapper.SysUserMapper;
import com.gz.steak.modules.system.service.SysLoginService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author 刘牌
 * @Date 2020/2/22 0022 19:05
 * @Version 1.0
 */
@Service("UserServiceImpl")
public class SysLoginServiceImpl implements SysLoginService {
    private final static Logger logger = LoggerFactory.getLogger(SysLoginServiceImpl.class);
    @Autowired
    private SysUserMapper userMapper;

    @Override
    public LoginModel getUserInfo(String username) throws BusinessException {
        List<LoginModel> userInfo = userMapper.getUserInfo(username);
        List<String> roleList = new ArrayList<>();
        List<Integer> roleIdList = new ArrayList<>();
        if (userInfo.size() == 0){
            throw new BusinessException(EmBusinessError.USER_NOT_EXIST);
        }
        for (LoginModel loginModel : userInfo){
            roleList.add(loginModel.getRoleName());
            roleIdList.add(loginModel.getId());
        }
        LoginModel loginModel = new LoginModel();
        loginModel.setUserName(userInfo.get(0).getUserName())
                .setRoleRemark(userInfo.get(0).getRoleRemark())
                .setRoles(roleList)
                .setUserPassword(userInfo.get(0).getUserPassword())
                .setRoleIdList(roleIdList);
        return loginModel;
    }


    @Override
    public void register(String username, String password,String ip,String address) {
        String date = DateFormat.dateToString(new Date());
        try {
            userMapper.register(username,password,ip,date,address);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

}
